package com.co.recommend.service.impl;

import com.co.recommend.dao.DetailMapper;
import com.co.recommend.dao.VideoMapper;
import com.co.recommend.dao.VideoStateMapper;
import com.co.recommend.service.NewVideoService;
import com.co.video.dto.VideoDto;
import com.co.video.pojo.Detail;
import com.co.video.pojo.Video;
import com.co.video.pojo.VideoState;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service

public class NewVideoServiceImpl implements NewVideoService {

    private static final Logger log = LoggerFactory.getLogger(NewVideoServiceImpl.class);

    @Autowired
    private DetailMapper detailMapper;
    @Autowired
    private VideoStateMapper videoStateMapper;
    @Autowired
    private VideoMapper videoMapper;

    @Override
    public List<VideoDto> findNewVideo(Integer row) {

        //得到最新的row条视频总信息
        List<Detail> newVideos = detailMapper.findNewVideo(row);

        if (newVideos == null || newVideos.size() <= 0) {
            log.info("没有最新的视频");
            return null;
        }
        List<VideoDto> list = new ArrayList<>();

        for (Detail newVideo : newVideos) {

            String aid = newVideo.getAid();
            //来查视频的状态信息
            VideoState videoState = new VideoState();
            videoState.setAid(aid);
            videoState = videoStateMapper.select(videoState).get(0);

            //查询videos
            Video video = new Video();
            video.setAid(aid);
            List<Video> videos = videoMapper.select(video);

            //数据封装
            VideoDto videoDto = new VideoDto();
            videoDto.setDetail(newVideo);
            videoDto.setVideoState(videoState);
            videoDto.setVideos(videos);

            list.add(videoDto);
        }

        return list;
    }
}
